//
//  LDFinderSearchVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit



class LDFinderSearchVC: UIViewController {
    
    var models:SearchViewsCellModels? = nil
    var searingModels:SearchViewsCellModels? = nil
    var searchBars:UISearchBar?
    var isSearching = false
    
    var tableView:LDBaseTableView?

    var tagsView:UIView!
    var searchResultView:UIView!
    
    var tagsVC:LDSearchTagsVC!
    var searchResultVC:LDFinderSearchResultVC!
    
    var tagsViewTag = 567
    var searchResultViewTag = 568
    
    var type:DDClientType = DDClientType.Cattle
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setNav()
        setupTableView()

    }
    
    override func viewWillDisappear(animated: Bool) {
        
        self.tableView?.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    }
    
    //
    func setupTableView() {
        
        
        //根view大小
        tagsVC  = LDSearchTagsVC()
        
        //点击标签回调
        tagsVC.getCategoryIDClosure { [weak self] (id, title) in
            
            if self == nil {
                return
            }
            self!.searchBars?.text = title
            //传入搜索文本
            self!.searchResultVC.tableViewResultVC.searchText = title
            self!.searchJobs(title)
            
            self?.showResultView()
        }
        
        tagsView = tagsVC.tableView
        tagsView.tag = tagsViewTag
        
        searchResultVC = LDFinderSearchResultVC()
        searchResultView = searchResultVC.view
        searchResultView.tag = searchResultViewTag
        
        addChildViewController(searchResultVC)
        addChildViewController(tagsVC)
        
        
        view.addSubview(searchResultView)
        view.addSubview(tagsView)
        view.backgroundColor = UIColor.whiteColor()
        //默认显示tagsView
        showTagsView()

    }
    
    //设置nav
    func setNav() {
        
        searchBars = UISearchBar()
        searchBars?.placeholder = "请输入你要搜索的内容"
        searchBars?.returnKeyType = .Search
        searchBars?.imageForSearchBarIcon(UISearchBarIcon.Bookmark, state: UIControlState.Normal)
        searchBars?.delegate = self
        
        
        for item in (searchBars?.subviews)! {
            
            if item.isKindOfClass(UITextField.self) {
                let textfileld = item as? UITextField
                textfileld?.contentHorizontalAlignment = .Left
                textfileld?.contentMode = .Left
            }
        }
        
        let bar = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Done, target: self, action: #selector(navBarCancel))
        
        
        bar.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(16)], forState: .Normal)
        bar.tintColor = LDFontColor.White
        navigationItem.rightBarButtonItem = bar
        navigationItem.leftBarButtonItem = nil//UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
        navigationItem.leftBarButtonItem?.customView?.removeFromSuperview()
        navigationItem.titleView = searchBars
        
    }

    override func viewWillAppear(animated: Bool) {
        
        navigationItem.setHidesBackButton(true, animated: false)
        navigationController?.navigationItem.leftBarButtonItem = nil
        rdv_tabBarController.setTabBarHidden(true, animated: false)
    }
    
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        searchBars?.resignFirstResponder()
    }
    
    func navBarCancel() {
        
        navigationController?.popViewControllerAnimated(false)
    }
    //默认搜索最新
    func searchJobs(text:String) {
        
        //判断版本 已做统一处理
        
//        if DDClientType.Cattle == DDClientInfoManager.clientType() {
        
            LDUserInfoNet.sharedInstance.searchJobs(page: 0, filterid: nil, filtertype: nil, text: text, success: {
                [weak self]
                (hasNextPage, jobsArr) in
                
                self?.searchResultVC.jobsInfoArr = jobsArr
                self?.searchResultVC.tableViewResultVC.searchText = text
                
            }) { (data) in
                
                LDShowHUB.shareInstance.showErr(data as? String ?? "失败")
            }
            
//        } else {
        
//            LDBossInfoNet.sharedInstance.searchJobFinders(page: 0, filterid: nil, filtertype: nil, text: text, success: {
//                
//                [weak self]
//                (hasNextPage, jobsArr) in
//                
//                self?.searchResultVC.jobsInfoArr = jobsArr
//                self?.searchResultVC.tableViewResultVC.searchText = text
//                
//                }, failure: { (data) in
//                    
//                LDShowHUB.shareInstance.showErr(data as? String ?? "失败")
//            })
//        }
    }
    
    //结果view
    func showResultView() {
        
        UIView.animateWithDuration(1) { [weak self] in
            
            self?.tagsView.hidden = true
            self?.searchResultView.hidden = false
        }
        
    }
    //标签view
    func showTagsView() {
        
        UIView.animateWithDuration(1) { [weak self] in
            
            self?.tagsView.hidden = false
            self?.searchResultView.hidden = true
        }
    }
    
}

extension LDFinderSearchVC:UISearchBarDelegate{
    //搜索时改变的字符
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        
        if !(searchBar.text?.characters.count > 0) {
            
        } else {
            

        }
    }
    
    //取消按钮
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        
    }
    //搜索开始
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        
        
    }
    
    //搜索结束
    func searchBarTextDidEndEditing(searchBar: UISearchBar) {
        
        searchBar.resignFirstResponder()
    }
    //
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        
        searchBars?.resignFirstResponder()
        
        let text = searchBar.text
        
        if !(text?.characters.count > 0) {
            
            showLDAlertView("搜索内容不能为空")
        }
        showResultView()
        
        searchJobs(text!)
    }
}